﻿package com.leqicheng.tenon.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.leqicheng.tenon.model.EntryInfoModel;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler,
        OnClickListener {

    private IWXAPI api;
    private Context mContext;
    private TextView mNavigationTitle;
    private ImageButton mNavigationBack;
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.mContext = this;
        this.api = WXAPIFactory.createWXAPI(this, "wx59ccca89485ac844", false);
        this.api.handleIntent(this.getIntent(), this);
    }

    @Override
    public void onReq(BaseReq arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onResp(BaseResp resp) {
        Intent intent = new Intent();
        switch (resp.errCode) {
        case BaseResp.ErrCode.ERR_OK:
            switch (resp.getType()) {
            case RETURN_MSG_TYPE_LOGIN :
                String token = ((SendAuth.Resp) resp).code;
                Toast.makeText(mContext, "验证授权中", Toast.LENGTH_SHORT).show();
                EntryInfoModel model = EntryInfoModel.getInstance();
                model.setThirdPartToken(token);
                intent.putExtra("login_status", true);
                intent.setAction("weixin complete");
                mContext.sendBroadcast(intent);
                finish();
                break;
            case RETURN_MSG_TYPE_SHARE:
                intent.putExtra("share_status", true);
                intent.setAction("weixin share complete");
                mContext.sendBroadcast(intent);
                finish();
                break;
            }
            break;
        case BaseResp.ErrCode.ERR_USER_CANCEL:
            Toast.makeText(mContext, "用户取消", Toast.LENGTH_SHORT).show();
            intent.putExtra("login_status", false);
            intent.setAction("weixin complete");
            mContext.sendBroadcast(intent);
            finish();
            break;
        case BaseResp.ErrCode.ERR_AUTH_DENIED:
            Toast.makeText(mContext, "授权失败", Toast.LENGTH_SHORT).show();
            intent.putExtra("login_status", false);
            intent.setAction("weixin complete");
            mContext.sendBroadcast(intent);
            finish();
            break;
        default:
            Toast.makeText(mContext, "分享失败", Toast.LENGTH_SHORT).show();
            intent.putExtra("login_status", false);
            intent.setAction("weixin complete");
            mContext.sendBroadcast(intent);
            finish();
            break;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    /**
     * 处理微信发出的向第三方应用请求app message
     * <p>
     * 在微信客户端中的聊天页面有“添加工具”，可以将本应用的图标添加到其中
     * 此后点击图标，下面的代码会被执行。Demo仅仅只是打开自己而已，但你可
     * 做点其他的事情，包括根本不打开任何页面
     */
    public void onGetMessageFromWXReq(WXMediaMessage msg) {
        Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(
                getPackageName());
        startActivity(iLaunchMyself);
    }

    /**
     * 处理微信向第三方应用发起的消息
     * <p>
     * 此处用来接收从微信发送过来的消息，比方说本demo在wechatpage里面分享
     * 应用时可以不分享应用文件，而分享一段应用的自定义信息。接受方的微信
     * 客户端会通过这个方法，将这个信息发送回接收方手机上的本demo中，当作
     * 回调。
     * <p>
     * 本Demo只是将信息展示出来，但你可做点其他的事情，而不仅仅只是Toast
     */
    public void onShowMessageFromWXReq(WXMediaMessage msg) {
        if (msg != null && msg.mediaObject != null
                && (msg.mediaObject instanceof WXAppExtendObject)) {
            WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject;
            Toast.makeText(this, obj.extInfo, Toast.LENGTH_SHORT).show();
        }
    }

}